import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/courseInfo',
    children: [
      {
        path: '/courseInfo',
        name: 'CourseInfo',
        component: () => import('@/views/courseInfo/index.vue'),
        meta: {
          title: '课程列表'
        }
      },
      {
        path: '/courseDetails',
        name: 'CourseDetails',
        component: () => import('@/views/courseInfo/courseDetails.vue'),
        meta: {
          title: '章节详情'
        }
      },
      {
        path: '/xueban_chat',
        name: 'XuebanChat',
        component: () => import('@/views/courseInfo/xueban_chat.vue'),
        meta: {
          title: '对话'
        }
      },
      {
        path: '/stu_exam',
        name: 'StuExam',
        component: () => import('@/views/courseInfo/stu_exam.vue'),
        meta: {
          title: '考核'
        }
      },
      {
        path: '/previewResource',
        name: 'PreviewResource',
        component: () => import('@/views/courseInfo/previewResource.vue'),
        meta: {
          title: '资源预览'
        }
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      whiteList: true,
      title: '登录'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/404/index.vue'),
    meta: { hidden: true }
  }
]

const router = createRouter({
  history: createWebHistory('/web/'),
  routes
})

// 白名单路由 - 不需要登录验证的页面
const whiteList = ['/login']

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查是否在白名单中
  if (whiteList.includes(to.path)) {
    next()
    return
  }

  // 直接放行所有路由，不进行token验证
  next()
})

export default router
